package org.artifact.csv;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReadConfig;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CsvBean {
	/** 名称 */
	private String name;
	/** 字段集 */
	private List<CsvField> fields = new ArrayList<>();
	/** 继承 */
	private String ext;
	
	public CsvBean(File file,String ext) {
		CsvReadConfig config = new CsvReadConfig();
    	config.setFieldSeparator(';');
    	CsvReader reader = new CsvReader(config);
    	CsvData csv = reader.read(file);
    	
    	String fileName =  StrUtil.removeSuffix(file.getName(), ".csv");
		// 2.转驼峰
		fileName = StrUtil.toCamelCase(fileName);
		// 3. 首字母大写
		fileName = StrUtil.upperFirst(fileName);
    	this.name = fileName;
    	
    	List<CsvRow> rows = csv.getRows();
    	CsvRow names = rows.get(0);		// 名称
    	CsvRow types = rows.get(1);		// 类型
    	CsvRow remarks = rows.get(2);	// 备注
    	CsvRow owns = rows.get(3);		// Server or Client
    	for (int i = 0; i < names.size(); i++) {
    		String own = owns.get(i);
    		if (StrUtil.containsIgnoreCase(own, "s")) {
    			CsvField field = new CsvField();
    			field.setName(names.get(i));
    			field.setType(types.get(i));
    			field.setRemark(remarks.get(i));
    			this.fields.add(field);
			}
		}
    	this.ext = ext;
	}
}
